// pages/classify/classify.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    protocal:'',
    showProtocal:false,
    show:false,
    name:'',
    address:'',
    classfiyText: '',
    classfiyId:'',
    phone: '',
    isAgree:false,
    businessImageUrl:'',
    businessImageUrl1: '',
    IDCardUrl:'',
    IDCardUrl1: '',
    businessLicenseUrl:'',
    businessLicenseUrl1: '',
    amount:'',
    check:false,
    type: []
  },


  open(){
    this.setData({show:true})
  },

  onClose(){
    this.setData({ showProtocal:false})
  },

  protocal(){
    util.request(api.protocal).then((res)=>{
      if(res.errno == 0){
        this.setData({
          protocal:res.data,
          showProtocal:true
        })
      }
    })
  },

  onConfirm(event) {
    // console.log(event)
    const { picker, value, index } = event.detail;
    // console.log(`当前值：${value}, 当前索引：${index}`);
    this.setData({
      classfiyText:value.text,
      classfiyId:value.value,
      show:false
    })
  },

  onCancel() {
    this.setData({show:false})
  },


  name(e){
    this.setData({name:e.detail.value})
  },

  address(e) {
    this.setData({ address: e.detail.value })
  },

  phone(e) {
    this.setData({ phone: e.detail.value })
  },
  amount(e){
    this.setData({ amount:e.detail.value})
  },
  businessImage(e) {
    var _this = this
    wx.chooseImage({
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        // console.log(res)
        // 上传多张只取第一张
        const images = res.tempFilePaths[0] 
        _this.setData({ businessImageUrl: images })
        wx.uploadFile({
          url: 'https://www.lpingougou.com/api/1/upload/upload',
          filePath: images,
          name: 'file',
          header: { 
            "Content-Type": "multipart/form-data",
            // 'X-Nideshop-Token': wx.getStorageSync('token')
             },
          success:ress=>{
            var res = JSON.parse(ress.data)
            if (res.code == 0) {
              _this.setData({ businessImageUrl1: res.data })
            } else {
              util.showErrorToast('上传失败，请稍后再试')
            }
          }
        })
      }
    })
  },
  IDCard(e) {
    var _this = this
    wx.chooseImage({
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        // 上传多张只取第一张
        
        const images = res.tempFilePaths[0]
        _this.setData({ IDCardUrl: images })
        wx.uploadFile({
          url: 'https://www.lpingougou.com/api/1/upload/upload',
          filePath: images,
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data",
            // 'X-Nideshop-Token': wx.getStorageSync('token')
          },
          success: ress => {
            var res = JSON.parse(ress.data)
            if (res.code == 0) {
              _this.setData({ IDCardUrl1: res.data })
            } else {
              util.showErrorToast('上传失败，请稍后再试')
            }
          }
        })
      }
    })
  },
  businessLicense(e) {
    var _this = this
    wx.chooseImage({
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        // console.log(res)
        // 上传多张只取第一张
        const images = res.tempFilePaths[0]
        _this.setData({ businessLicenseUrl: images })
        wx.uploadFile({
          url: 'https://www.lpingougou.com/api/1/upload/upload',
          filePath: images,
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data",
            // 'X-Nideshop-Token': wx.getStorageSync('token')
          },
          success: ress => {
            var res = JSON.parse(ress.data)
            if(res.code == 0){
              _this.setData({businessLicenseUrl1:res.data})
            }else{
              util.showErrorToast('上传失败，请稍后再试')
            }
          }
        })
      }
    })
  },
  checkboxChange(){
    if(this.data.isAgree == false){
      this.setData({isAgree : true})
    }else{
      this.setData({ isAgree: false })
    }
  },
  onSubmit(e){
    var formId = e.detail.formId
    var _this = this
    var name = this.data.name
    var address = this.data.address
    var classfiy = this.data.classfiyId
    var phone = this.data.phone
    var businessImageUrl1 = this.data.businessImageUrl1
    var IDCardUrl1 = this.data.IDCardUrl1
    var businessLicenseUrl1 = this.data.businessLicenseUrl1
    var amount = this.data.amount
    
    

    if (!name){
      util.alert('请填写店铺名称')
      return
    }else if(!address){
      util.alert('请填写地址')
      return
    } else if (!phone) {
      util.alert('请填写联系电话')
      return
    } else if (!(/^1[34578]\d{9}$/.test(phone))) {
      util.alert('请输入正确的手机号码')
      return
    } else if (!businessImageUrl1) {
      util.alert('请上传店铺头照')
      return
    } else if (!IDCardUrl1) {
      util.alert('请上传法人身份证')
      return
    } else if (!businessLicenseUrl1) {
      util.alert('请上传营业执照')
      return
    }else if(this.data.isAgree == false){
      util.alert('请同意入驻协议')
      return
    }else{
      let data = { storeName: name, storeAddress: address, type: classfiy, mobile: phone, banner: businessImageUrl1, bossIdCard: IDCardUrl1, businessLicense: businessLicenseUrl1, form_id:formId}
      util.post(api.businessEnter,data).then((res)=>{
        if(res.code == 0){
          _this.setData({check:true})
        }else{
          util.showErrorToast(res.msg)
        }
      })
    }
  },

  deleteBusinessLicenseUrl(){
    this.setData({ businessLicenseUrl: '', businessLicenseUrl1: '',})
  },
  deleteIDCardUrl(){
    this.setData({ IDCardUrl: '', IDCardUrl1: '' })
  },
  deleteBusinessImageUrl() {
    this.setData({ businessImageUrl: '', businessImageUrl1: '' })
  },

  closeCheck(){
    this.setData({ check: false })
    wx.switchTab({
      url: '/pages/me/me',
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    util.request(api.storeMenu).then((res)=>{
      if (res.errno == 0){
        var arr = []
        res.data.forEach(e=>{
          if(e.code != -1){
            e.text = e.name 
            e.value = e.code
            arr.push(e)
          }
        })
        this.setData({ type:arr})
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})